package com.gx.smart.building.ui.message

import android.content.Intent
import android.view.ViewGroup
import com.gx.smart.base.BaseAdapter
import com.gx.smart.base.BaseMVVMRefreshActivity
import com.gx.smart.building.R
import com.gx.smart.building.databinding.ActivityMessageBinding
import com.gx.smart.building.ui.message.adapter.MessageAdapter
import com.gx.smart.building.ui.message.event.ItemMessageEvent
import com.gx.smart.building.ui.message.viewmodel.MessageViewModel
import com.gx.smart.lib.http.api.response.Message
import com.scwang.smartrefresh.layout.api.RefreshLayout
import org.greenrobot.eventbus.EventBus

class MessageActivity : BaseMVVMRefreshActivity<MessageViewModel>(),
    BaseAdapter.OnItemClickListener<Message> {
    private lateinit var viewBinding: ActivityMessageBinding
    private lateinit var adapter: MessageAdapter
    override fun onBindViewModel(): Class<MessageViewModel> = MessageViewModel::class.java

    override fun onBindLayout(): Int = R.layout.activity_message

    override fun getToolbarTitle(): String = "我的消息"

    override fun initContentView(root: ViewGroup) {
        viewBinding = ActivityMessageBinding.inflate(layoutInflater, root, true)
        adapter = MessageAdapter(this)
        adapter.setItemClickListener(this)
        initView(viewBinding.recyclerView, adapter, viewBinding.refreshLayout)
    }

    override fun onRefresh(refreshlayout: RefreshLayout) {
        super.onRefresh(refreshlayout)
        doAction()
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        super.onLoadMore(refreshLayout)
        doAction()
    }

    private fun doAction() {
        viewModel.messageRecordQuery(this.currentPage).observe(this) { list ->
            handleResult(list)
        }
    }

    override fun onResume() {
        super.onResume()
        viewBinding.refreshLayout.autoRefresh()
    }

    override fun onItemClick(item: Message, position: Int) {
        startActivity(Intent(this, MessageDetailActivity::class.java))
        EventBus.getDefault().postSticky(ItemMessageEvent(item))
    }
}